package com.vma.jifu.backsys.controller;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.jifu.bizModel.model.Message2User;
import com.vma.jifu.bizModel.model.MessageLogs;
import com.vma.jifu.bizModel.model.SystemMessage;
import com.vma.jifu.bizModel.model.User;
import com.vma.tools.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kitabi on 2017/7/10.
 */
public class MessageController extends BaseController {

    public void qryMessage(){
        String status = getPara("status");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");

        String start = getPara("start");
        String end = getPara("end");

        String title = getPara("title");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(status != null && "".equals(status)){
            condition += "and t.status = ?";
            param.add(status);
        }

        if(start != null && !"".equals(start)){
            condition += "and t.create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and t.create_time < ? ";
            param.add(end);
        }

        if(title == null || "".equals(title)){

        }else {
            condition += "and t.title like ? ";
            param.add("%"+title+"%");
        }



        Page<SystemMessage> joinFee = SystemMessage.dao.paginate(pageNo,pageSize,"select * ",
                " from system_message t " +
                        "where type =1 and status = 1 "+condition +"order by id desc"
                ,param.toArray());


        renderJson(ReturnForm(joinFee.getList(),joinFee.getTotalRow()));
    }


    public void addMessage(){
        String title = getPara("title");
        String content= getPara("content");

        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setCreateTime(new Date()).setTitle(title).setType(1).setStatus(1).setContent(content).save();

        List<User> users = User.dao.find("select * from user where status = 1");

        for(User temp : users){
            Message2User message2User= new Message2User();
            message2User.setCreateTime(new Date()).setMessageId(systemMessage.getId()).setUserId(temp.getId()).save();
        }

        renderJson(ReturnsUtil.returnSuccess("新增成功"));
    }

    public void delMessage(){
        String id = getPara("id");

        String[]  ids= id.split(",");

        for(String temp : ids){
            SystemMessage.dao.deleteById(temp);
        }

        renderJson(ReturnsUtil.returnSuccess("删除成功"));
    }

    public void qryMessageDetail(){
        String id =getPara("id");

        if(id != null && !"".equals(id)){
            SystemMessage tgArticle = SystemMessage.dao.findById(id);
            setAttr("initData",tgArticle);
        }

        renderVelocity("/newshop/system/message/addMessage.html");
    }
}
